﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using RVEAd.Business;
using RVEAd.Business.Model;
using RVEAd.Helpers;
using System.Linq;

namespace RVEAd.Forms
{
    public partial class FormVizualizarePlanificare : Form
    {
        private RveBL _rveBl;

        public DateTime CurrentDate
        {
            get
            {
                return new DateTime(Convert.ToInt32(comboAn.SelectedItem), Convert.ToInt32(comboLuna.SelectedValue), 1);
            }
        }


        public FormVizualizarePlanificare()
        {
            InitializeComponent();
            this.Load += new EventHandler(FormVizualizarePlanificare_Load);
        }

        void FormVizualizarePlanificare_Load(object sender, EventArgs e)
        {
            _rveBl = new RveBL();

            dataGridClients.AutoGenerateColumns = false;
            dataGridPlanificari.AutoGenerateColumns = false;

            LoadFilterDate();
            LoadClients();
        }

        private void LoadFilterDate()
        {
            // bind luna
            comboLuna.DataSource = Common.MonthsList;
            comboLuna.DisplayMember = "MonthText";
            comboLuna.ValueMember = "MonthValue";
            comboLuna.SelectedValue = DateTime.Now.Month;

            // bind year
            comboAn.DataSource = Common.YearsList;
            comboAn.SelectedItem = DateTime.Now.Year;
        }

        private void LoadClients()
        {
            dataGridClients.DataSource = _rveBl.GetClients();
        }



        void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void btnViewDetalii_Click(object sender, EventArgs e)
        {
            FormDetaliiPlanDaily dailyPlan = new FormDetaliiPlanDaily();

            dailyPlan.SelectedMonth = Convert.ToInt32(comboLuna.SelectedValue);
            dailyPlan.SelectedYear = Convert.ToInt32(comboAn.SelectedItem);
            dailyPlan.ShowDialog();
        }

        void dataGridClients_SelectionChanged(object sender, EventArgs e)
        {
            Client client = dataGridClients.CurrentRow.DataBoundItem as Client;

            if (client != null)
            {
                dataGridPlanificari.DataSource = _rveBl.GetClientAds(client.ID, DateTime.Now);
            }
        }

        private void comboLuna_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateClients();
        }

        private void comboAn_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateClients();
        }

        private void UpdateClients()
        {
            var datasource = ((List<Client>)dataGridClients.DataSource);

            if (datasource != null && datasource.Count > 0)
            {
                dataGridPlanificari.DataSource = _rveBl.GetClientAds(datasource.ElementAt(0).ID, CurrentDate);
            }
        }


    }
}
